package com.xindaoapp.happypet.adapter;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.Message;
import com.xindaoapp.happypet.ui.PostDetailActivity;
import com.xindaoapp.happypet.utils.BitmapUtil;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.TimeFormat;
import com.xindaoapp.happypet.view.CircleImageView;
import com.xindaoapp.happypet.view.RoundImageView2;

public class MainNewAdapter extends BaseAdapter {
    private LinkedList<Message> arrays = new LinkedList<Message>();
    private final Context context;
    private int photoSize = 80;
    private int displayWidth = 720;

    public MainNewAdapter(Context context) {
        super();
        this.context = context;
    }

    public void addItems(List<Message> array) {
        arrays.addAll(array);
    }

    public void clearItems() {
        arrays.clear();
    }

    public void setArrays(LinkedList<Message> arrays) {
        this.arrays = arrays;
    }

    @Override
    public int getCount() {
        return arrays.size();
    }

    @Override
    public Object getItem(int position) {
        return arrays.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("static-access")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null && convertView instanceof RelativeLayout) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(context, R.layout.menu_message_item, null);
            holder = new ViewHolder();
            holder.ll = (LinearLayout) convertView.findViewById(R.id.ll_pet_icon);
            holder.usericon = (CircleImageView) convertView.findViewById(R.id.user_icon);
            holder.username = (TextView) convertView.findViewById(R.id.user_name);
            holder.title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.type = (ImageView) convertView.findViewById(R.id.iv_top_right);
            holder.look = (TextView) convertView.findViewById(R.id.look_time);
            holder.care = (TextView) convertView.findViewById(R.id.care_time);
            holder.message = (TextView) convertView.findViewById(R.id.message_time);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.iv_top_right = (ImageView) convertView.findViewById(R.id.iv_top_right);
            convertView.setTag(holder);
        }
        holder.title.setText(arrays.get(position).subject);
        holder.username.setText(arrays.get(position).author);
        holder.look.setText(arrays.get(position).views);
        holder.care.setText(arrays.get(position).recommend_add);
        holder.message.setText(arrays.get(position).replies);
        holder.time.setText(TimeFormat.getInstance().format(arrays.get(position).addtime));

        String time = CommonUtil.formatDate();
        String year = time.substring(0, 4);
        String hour = time.split(" ")[1].split(":")[0];
        String min = time.split(" ")[1].split(":")[1];

        String mTime = arrays.get(position).addtime;
        String mYear = mTime.substring(0, 4);
        String mMonth = mTime.split(" ")[0].split("-")[1];
        String mDay = mTime.split(" ")[0].split("-")[2];
        String mHour = mTime.split(" ")[1].split(":")[0];
        String mMin = mTime.split(" ")[1].split(":")[1];

        if (year.equals(mYear)) {// 说明是今年
            if (arrays.get(position).addtime.split(" ")[0].equals(time.split(" ")[0])) {// 说明是今天
                holder.time.setText(mHour + ":" + mMin);
            } else {
                holder.time.setText(mMonth + "-" + mDay);
            }
        } else {
            holder.time.setText(mTime.split(" ")[0]);
        }

        DisplayImageOptions defaultOptions =
                new DisplayImageOptions.Builder().resetViewBeforeLoading(true)
                        .showStubImage(R.drawable.picture_loading)
                        .showImageForEmptyUri(R.drawable.picture_loading)
                        .showImageOnFail(R.drawable.picture_loading)
                        .imageScaleType(ImageScaleType.NONE).bitmapConfig(Bitmap.Config.RGB_565)
                        .displayer(new RoundedBitmapDisplayer(10)).cacheInMemory(true)
                        .cacheOnDisc(true).build();

        displayWidth = getWidth();
        if (displayWidth < 720) {
            photoSize = 64;
        }
        if (arrays.get(position).isvideo != null && arrays.get(position).isvideo.equals("1")) {
            holder.ll.removeAllViews();
            RelativeLayout.LayoutParams lp1 =
                    new RelativeLayout.LayoutParams(CommonUtil.dip2px(context, 200),
                            CommonUtil.dip2px(context, 200));
            View sView = View.inflate(context, R.layout.image_pet_icon_shipin, null);
            RoundImageView2 iv = (RoundImageView2) sView.findViewById(R.id.pet_icon);
            iv.setLayoutParams(lp1);
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            if (CommonParameter.isLoadImg) {
                ImageLoader.getInstance().displayImage(arrays.get(position).videocover, iv,
                        defaultOptions);
            } else {
                iv.setImageResource(R.drawable.picture_loading);
            }
            LayoutParams lp =
                    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            sView.setLayoutParams(lp);
            holder.ll.addView(sView);
        } else if (arrays.get(position).isvideo == null || arrays.get(position).isvideo.equals("0")) {
            int size = arrays.get(position).pics.size();
            if (size == 0) {
                holder.ll.removeAllViews();
            }
            if (size == 1) {
                holder.ll.removeAllViews();
                View view = View.inflate(context, R.layout.image_pet_icon, null);
                RoundImageView2 iv1 = (RoundImageView2) view.findViewById(R.id.pet_icon);
                RelativeLayout.LayoutParams Rp1 =
                        new RelativeLayout.LayoutParams(CommonUtil.dip2px(context, photoSize),
                                CommonUtil.dip2px(context, photoSize));
                iv1.setLayoutParams(Rp1);
                iv1.setScaleType(ImageView.ScaleType.CENTER_CROP);
                if (CommonParameter.isLoadImg) {
                    ImageLoader.getInstance().displayImage(arrays.get(position).pics.get(0).url,
                            iv1, defaultOptions);
                } else {
                    iv1.setBackgroundResource(R.drawable.picture_loading);
                }
                LayoutParams lp1 =
                        new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                view.setLayoutParams(lp1);

                holder.ll.addView(view);
            } else if (size == 2) {
                holder.ll.removeAllViews();
                for (int i = 0; i < 2; i++) {
                    LinearLayout view2 =
                            (LinearLayout) View.inflate(context, R.layout.image_pet_icon, null);
                    RoundImageView2 iv2 = (RoundImageView2) view2.findViewById(R.id.pet_icon);
                    android.view.ViewGroup.LayoutParams Rp2 = iv2.getLayoutParams();
                    Rp2.width = CommonUtil.dip2px(context, photoSize);
                    Rp2.height = CommonUtil.dip2px(context, photoSize);
                    iv2.setLayoutParams(Rp2);
                    iv2.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    if (CommonParameter.isLoadImg) {
                        ImageLoader.getInstance().displayImage(
                                arrays.get(position).pics.get(i).url, iv2, defaultOptions);
                    } else {
                        iv2.setBackgroundResource(R.drawable.picture_loading);
                    }
                    LayoutParams lp2 =
                            new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    if (i > 0) {
                        lp2.leftMargin = 15;
                    }
                    view2.setLayoutParams(lp2);
                    holder.ll.addView(view2);
                }
            } else if (size >= 3) {
                holder.ll.removeAllViews();
                for (int i = 0; i < 3; i++) {
                    LinearLayout view3 =
                            (LinearLayout) View.inflate(context, R.layout.image_pet_icon, null);
                    RoundImageView2 iv3 = (RoundImageView2) view3.findViewById(R.id.pet_icon);
                    android.view.ViewGroup.LayoutParams Rp3 = iv3.getLayoutParams();
                    Rp3.width = CommonUtil.dip2px(context, photoSize);
                    Rp3.height = CommonUtil.dip2px(context, photoSize);
                    iv3.setLayoutParams(Rp3);
                    iv3.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    if (CommonParameter.isLoadImg) {
                        ImageLoader.getInstance().displayImage(
                                arrays.get(position).pics.get(i).url, iv3, defaultOptions);
                    } else {
                        iv3.setBackgroundResource(R.drawable.picture_loading);
                    }
                    LayoutParams lp3 =
                            new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    view3.setLayoutParams(lp3);
                    if (i > 0) {
                        lp3.leftMargin = 15;
                    }
                    holder.ll.addView(view3);
                }
            }
        }
        if (CommonParameter.isLoadImg) {
            ImageLoader.getInstance().displayImage(arrays.get(position).userface, holder.usericon,
                    BitmapUtil.getPersonLoading());
        } else {
            holder.usericon.setBackgroundResource(R.drawable.person_loading);
        }
        if (Integer.valueOf(arrays.get(position).digest) > 0) {
            holder.iv_top_right.setBackgroundResource(R.drawable.jing);
        } else {
            holder.iv_top_right.setBackgroundColor(Color.WHITE);
        }
        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PostDetailActivity.class);
                intent.putExtra(Constants.KEY_POST_TID, arrays.get(position).tid);
                context.startActivity(intent);
            }
        });
        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PostDetailActivity.class);
                intent.putExtra(Constants.KEY_POST_TID, arrays.get(position).tid);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        LinearLayout ll;
        CircleImageView usericon;
        TextView username;
        TextView title;
        ImageView type;
        TextView look;
        TextView care;
        TextView message;
        TextView time;
        ImageView iv_top_right;

    }

    private int getWidth() {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return wm.getDefaultDisplay().getWidth();
    }
}
